// 同步计算文件大小
const fs = require('fs')
const path = require('path')

function calcFileSizeSync (dirPath, cb) {
  let fileSize = 0

  function getFileSize (dirPath) {
    const statObj = fs.statSync(dirPath)
    if (statObj.isDirectory()) {
      const files = fs.readdirSync(dirPath)
      const dirs = files.map(item => path.join(dirPath, item))
      for (let i = 0; i < dirs.length; i++) {
        getFileSize(dirs[i])
      }
    } else {
      fileSize += statObj.size
    }
  }
  getFileSize(dirPath)
  cb && cb(fileSize)
}

calcFileSizeSync('test', size => {
  console.log("同步计算文件大小：" + size)
})

